package com.wenwu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.Resource;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.dao.ResourceMapper;
import com.wenwu.service.ResourceService;

/**
 * @project ATHome-Manager-Service
 * @class ResourceServiceImpl.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年3月4日 上午11:19:45
 * @description 资源管理模块
 */
@Service
public class ResourceServiceImpl implements ResourceService {
	@Autowired
	private ResourceMapper resourceMapper;

	public List<Resource> selectAllList() throws RelicsException {
		try {
			return resourceMapper.selectByExample(null);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int addResource(Resource resource) throws RelicsException {
		try {
			return resourceMapper.insertSelective(resource);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int updateResource(Resource resource) throws RelicsException {
		try {
			if (resource.getSourceLevel()==0) {//修改其子节点的启用状态
				return resourceMapper.updateByPrimaryKey(resource)+resourceMapper.updateChildren(resource);
			}else{
				return resourceMapper.updateByPrimaryKey(resource);
			}
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public Resource selectByKey(Integer sourceId) throws RelicsException {
		try {
			return resourceMapper.selectByPrimaryKey(sourceId);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int deleteResourceByKey(List<?> list) throws RelicsException {
		try {
			return resourceMapper.deleteResourceByKey(list);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public List<Resource> selectResourceByRoleid(List<?> ids) throws RelicsException {
		try {
			return resourceMapper.selectResourceByRoleid(ids);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}
}
